package com.webex.ta.hydra.gui.menu;

import com.webex.ta.hydra.gui.GuiPackage;
import com.webex.ta.hydra.gui.MenuFactory;
import com.webex.ta.hydra.gui.action.ActionNames;
import com.webex.ta.hydra.gui.action.KeyStrokes;
import com.webex.ta.hydra.gui.table.HydraTable;
import com.webex.ta.hydra.core.Command;
import com.webex.ta.hydra.util.HydraUtils;
import com.webex.ta.hydra.clipboard.ClipBoard;

import javax.swing.*;

/**
 * Author: Vegaz
 * Date: 2010-11-9
 * Time: 9:56:49
 */
public class ContextMenuProvider implements DynamicMenuProvider {
    public boolean updateEveryTime() {
        return true;
    }

    public void update(JMenu menu) {
        HydraTable table = GuiPackage.getInstance().getHydraTable();
        if (table != null && table.getSelectedRow() >=0 ) {
            Command cmd = table.getModel().getCommandAt(table.getSelectedRow());
            if (HydraUtils.isController(cmd)) {
                updateControllerMenu(menu);
            } else if (HydraUtils.isProcessor(cmd)) {
                updateActionMenu(menu);
            } else {
                updateActionMenu(menu);
            }
        } else {
            updateDefaultMenu(menu);
        }

        JMenuItem copy = MenuFactory.makeMenuItem("Copy", "Copy", ActionNames.COPY_LINE, KeyStrokes.COPY_LINE);
        copy.setIcon(HydraUtils.getImage("copy.gif"));
        JMenuItem paste = MenuFactory.makeMenuItem("Paste", "Paste", ActionNames.PASTE_LINE, KeyStrokes.PASTE_LINE);
        paste.setIcon(HydraUtils.getImage("paste.gif"));
        JMenuItem remove = MenuFactory.makeMenuItem("Remove", "Remove", ActionNames.REMOVE, KeyStrokes.REMOVE);
        remove.setIcon(HydraUtils.getImage("remove.gif"));
        JMenuItem collapse = MenuFactory.makeMenuItem("Collapse", "Collapse", ActionNames.COLLAPSE, KeyStrokes.COLLAPSE);
        JMenuItem expand = MenuFactory.makeMenuItem("Expand", "Expand", ActionNames.EXPAND, KeyStrokes.EXPAND);
        menu.add(copy);
        menu.add(paste);
        menu.add(remove);
        menu.add(collapse);
        menu.add(expand);


        if (table != null) {
            if (table.getSelectedRow() < 0)
                copy.setEnabled(false);
            paste.setEnabled(ClipBoard.getInstance().hasContent());

            if (table.isLocked()) {
                copy.setEnabled(false);
                paste.setEnabled(false);
                remove.setEnabled(false);
            }
        }

        menu.addSeparator();

        JMenuItem copyCell = MenuFactory.makeMenuItem("Copy values", "Copy valules", ActionNames.COPY);
        menu.add(copyCell);
        JMenuItem pasteCell = MenuFactory.makeMenuItem("Paste values", "Paste values", ActionNames.PASTE);
        menu.add(pasteCell);
        JMenuItem deleteCell = MenuFactory.makeMenuItem("Delete values", "Delete values", ActionNames.REMOVE_VALUES, KeyStrokes.REMOVE_VALUES);
        menu.add(deleteCell);

        menu.addSeparator();

        JMenuItem annotate = MenuFactory.makeMenuItem("Edit annotation", "Edit annotation", ActionNames.EDIT_ANNOTATION);
        annotate.setIcon(HydraUtils.getImage("annotate.gif"));
        menu.add(annotate);

        JMenuItem enable = MenuFactory.makeMenuItem("Enable/Disable", "Enable/Disable", ActionNames.TOGGLE_COMMENT, KeyStrokes.TOGGLE_COMMENT);
        menu.add(enable);
    }

    private void updateDefaultMenu(JMenu menu) {
        DynamicMenu insert = new DynamicMenu("Insert", "Insert");
        insert.setProvider(new NewItemMenuProvider(ActionNames.INSERT, NewItemMenuProvider.ALL));
        menu.add(MenuFactory.makeMenuItem("Insert empty", "Insert empty", ActionNames.INSERT_EMTPY));
        menu.add(insert);
    }

    private void updateControllerMenu(JMenu menu) {
        menu.add(MenuFactory.makeMenuItem("Insert empty", "Insert empty", ActionNames.INSERT_EMTPY));

        DynamicMenu addChild = new DynamicMenu("Add child", "Add child");
        addChild.setProvider(new NewItemMenuProvider(ActionNames.ADD_CHILD, NewItemMenuProvider.ALL));

        DynamicMenu insertAfter = new DynamicMenu("Insert after", "Insert after");
        insertAfter.setProvider(new NewItemMenuProvider(ActionNames.INSERT_AFTER, NewItemMenuProvider.ALL));

        DynamicMenu insertBefore = new DynamicMenu("Insert before", "Insert before");
        insertBefore.setProvider(new NewItemMenuProvider(ActionNames.INSERT_BEFORE, NewItemMenuProvider.ALL));

        menu.add(addChild);
        menu.add(insertAfter);
        menu.add(insertBefore);
    }

    private void updateActionMenu(JMenu menu) {
        menu.add(MenuFactory.makeMenuItem("Insert empty", "Insert empty", ActionNames.INSERT_EMTPY));

        DynamicMenu insertAfter = new DynamicMenu("Insert after", "Insert after");
        insertAfter.setProvider(new NewItemMenuProvider(ActionNames.INSERT_AFTER, NewItemMenuProvider.ALL));

        DynamicMenu insertBefore = new DynamicMenu("Insert before", "Insert before");
        insertBefore.setProvider(new NewItemMenuProvider(ActionNames.INSERT_BEFORE, NewItemMenuProvider.ALL));
        menu.add(insertAfter);
        menu.add(insertBefore);
    }
}
